<!--
 作者：ex_baiyc
 日期：2016-11-3
 功能：首页-知己彼-品牌对比-品牌销售对比
 参数：
 事件：
-->
<template>
  <div class="the-sales">
    <toolbar :view-title="viewTitle">
      <div
        class="leftButton"
        type="button"
        slot="left-button"
        v-link='{path: "/peer"}'>&nbsp;
      </div>
      <div
        class="rightButton"
        type="button"
        slot='right-button'>
        <div class="filterButton" @click="showFilter" v-show="!filterOpenState">
          <span class="filterIcon" :page-id="routeKey">&nbsp;</span>
          <span>筛选</span>
        </div>
        <div class="menuButton" @click="showSlideMenu" v-show="!filterOpenState">
          <span class="menuIcon">&nbsp;</span>
          <span>菜单</span>
        </div>
        <span v-show="filterOpenState" @click="doFilter" class="filterOver">完成</span>
      </div>
    </toolbar>
    <div class="the-sales-content">

      <select-for-brand :select-a-text="'选择品牌A'"
                        :select-b-text="'选择品牌B'"
                        :default-org-id="defaultOrgId"
                        :default-brand="defaultBrand"
                        :default-category="defaultCategory"
                        :default-category-id="defaultCategoryId"></select-for-brand>
      <div class="filter-message-box clearfix">
        <a  v-if="defaultCategory !== ''" class="filter-condition" @click="showFilter('pinlei')" href="javascript:;">{{defaultCategory}}</a>
        <span>
          <a v-if="UrlParams.platform !== ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;" v-text="UrlParams.platform"></a>
          <a v-if="UrlParams.platform === ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">所有平台</a>
        </span>
        <a class="filter-condition" @click="showFilter('shijian')" href="javascript:;">{{UrlParams.startTime | formatDate}} - {{UrlParams.endTime | formatDate}}</a>
      </div>
      <div class="content">
        <section class="section">
          <p class="sub-title">{{chartData1.subTitle}}</p>
          <line-chart-view :chart-data='chartData1' :chart-height="'64vh'" :chart-width="'100%'"></line-chart-view>
        </section>

        <section class="section">
          <p class="sub-title">{{chartData2.subTitle}}</p>
          <line-chart-view :chart-data='chartData2' :chart-height="'54vh'" :chart-width="'100%'"></line-chart-view>
        </section>

        <section class="section">
          <p class="sub-title">{{chartData3.subTitle}}</p>
          <line-chart-view-percent :chart-data='chartData3' :chart-height="'54vh'" :chart-width="'100%'"></line-chart-view-percent>
        </section>

        <section class="section">
          <p class="sub-title">{{chartData4.subTitle}}</p>
          <line-chart-view-percent :chart-data='chartData4' :chart-height="'54vh'" :chart-width="'100%'"></line-chart-view-percent>
        </section>
      </div>
    </div>
    <filter-view v-show="filterOpenState"
                 :condition="conditions"
                 :choose-platforms="choosePlatforms"
                 :page-id="routeKey"
                 :default-start-date="UrlParams.startTime"
                 :default-end-date="UrlParams.endTime"
                 :default-org-id="defaultOrgId"
                 :default-category="defaultCategory"
                 :default-category-id="defaultCategoryId"></filter-view>
    <slide-menu v-show="menuOpenState"></slide-menu>
    <page-turning :mkey="routeKey" :page-id="'peer'"></page-turning>
    <div class="loading-box">
      <img src="../../../../static/img/loading.gif" class="loading">
    </div>
    <confirm :show.sync="timeoutStatus"
   title="加载失败,您可以"
   cancel-text="返回首页"
   confirm-text="重新加载"
   @on-cancel="this.$route.router.go('/home')"
   @on-confirm="reload"></confirm>
  </div>
</template>

<script>
  import Toolbar from '../../common/Toolbar.vue'
  import FilterView from '../../common/FilterView.vue'
  import SelectForBrand from '../../peer/selectForBrand.vue'
  import LineChartView from '../../common/LineChartView.vue'
  import LineChartViewPercent from '../../common/LineChartViewPercent.vue'
  import SlideMenu from '../../peer/SlideMenu.vue'
  import pageTurning from '../../common/pageTurning.vue'
  import Confirm from 'vux/dist/components/Confirm'

  export default{
    components: {
      Toolbar,
      FilterView,
      SelectForBrand,
      LineChartView,
      LineChartViewPercent,
      SlideMenu,
      pageTurning,
      Confirm
    },
    data () {
      return {
        timeoutStatus: false,
        routeKey: '',
        menuOpenState: false,
        viewTitle: '',
        filterOpenState: false,
        choosePlatforms: [],
        defaultBrand: '',
        defaultOrgId: '',
        defaultCategory: '',
        defaultCategoryId: '',
        chartData1: {
          subTitle: '',
          legend: [],
          xAxisData: [],
          yAxisName: '               销量',
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'line',
              data: []
            },
            {
              name: '',
              type: 'line',
              data: []
            }
          ]
        },
        chartData2: {
          subTitle: '',
          legend: [],
          xAxisData: [],
          yAxisName: '               销额(万)',
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'line',
              data: []
            },
            {
              name: '',
              type: 'line',
              data: []
            }
          ]
        },
        chartData3: {
          subTitle: '',
          legend: [],
          xAxisData: [],
          yAxisName: '',
          xAxisDataInterval: 100,
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'line',
              data: []
            },
            {
              name: '',
              type: 'line',
              data: []
            }
          ]
        },
        chartData4: {
          subTitle: '',
          legend: [],
          xAxisData: [],
          yAxisName: '',
          xAxisDataInterval: 100,
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'line',
              data: []
            },
            {
              name: '',
              type: 'line',
              data: []
            }
          ]
        },
//
        UrlParams: {
          pageId: '65',
          mkey: 'brandSalesComparison',
          dateSeg: 'other',
          startTime: '',
          endTime: '',
          category_id: '',
          category: '',
          brand1: '',
          brand2: '',
          brand: '',
          org_code: '',
          platform: ''
        }
      }
    },
    methods: {
      reload: function () {
        window.location.reload()
      },
      showFilter: function () {
        this.filterOpenState = true
      },
      showSlideMenu: function () {
        this.menuOpenState = !this.menuOpenState
        var self = this
        setTimeout(function () {
          self.$broadcast('slideStart')
        }, 0)
      },
      doFilter: function () {
        this.$broadcast('doFilter')
        this.filterOpenState = false
      },
//      获取页面数据后给图表填充数据
      getData: function (params) {
//      this.$http.get('./static/data/peer/brand/brandComparison.json').then(function (res) {
        this.$http.post(window.interface.peerData, params, {_timeout: 30000}).then((res) => {
          if (res.json().error) {
            this.timeoutStatus = true
            return
          }
          document.getElementsByClassName('loading-box')[0].style.display = 'none'
          this.chartData1.xAxisData = []
          this.chartData1.series[0].data = []
          this.chartData1.series[1].data = []
          this.chartData2.xAxisData = []
          this.chartData2.series[0].data = []
          this.chartData2.series[1].data = []
          this.chartData3.xAxisData = []
          this.chartData3.series[0].data = []
          this.chartData3.series[1].data = []
          this.chartData4.xAxisData = []
          this.chartData4.series[0].data = []
          this.chartData4.series[1].data = []

          var data = res.json()
          // if (!data.chartDatas[0] || data.chartDatas[0].detail.length === 0 || !data.chartDatas[1] || data.chartDatas[1].detail.length === 0 || !data.chartDatas[2] || data.chartDatas[2].detail.length === 0 || !data.chartDatas[3] || data.chartDatas[3].detail.length === 0) {
          //   window.alert('当前条件下无数据,请重新选择!')
          //   this.filterOpenState = true
          //   return
          // }
          this.choosePlatforms = data.platformCnd || data.platform
          this.UrlParams.startTime = data.startTime || ''
          this.UrlParams.endTime = data.endTime || ''
          this.defaultBrand = data.brand || ''
          this.defaultOrgId = data.org_code || ''
          this.defaultCategory = this.UrlParams.category = data.category || ''
          this.defaultCategoryId = this.UrlParams.category_id = data.category_id || ''
          this.viewTitle = data.title
          this.$broadcast('loadDataDone')
          var chart1 = data.chartDatas[0].detail
          var chart2 = data.chartDatas[1].detail
          var chart3 = data.chartDatas[2].detail
          var chart4 = data.chartDatas[3].detail

          this.chartData1.yAxisName = data.chartDatas[0].saleUnit ? '               销量' + data.chartDatas[0].saleUnit : '               销量'

          this.chartData1.subTitle = data.chartDatas[0].subTitle
          this.chartData2.subTitle = data.chartDatas[1].subTitle
          this.chartData3.subTitle = data.chartDatas[2].subTitle
          this.chartData4.subTitle = data.chartDatas[3].subTitle

          this.chartData1.series[0].name = chart1[0].brand
          this.chartData1.series[1].name = chart1[1].brand
          this.chartData2.series[0].name = chart2[0].brand
          this.chartData2.series[1].name = chart2[1].brand
          this.chartData3.series[0].name = chart3[0].brand
          this.chartData3.series[1].name = chart3[1].brand
          this.chartData4.series[0].name = chart4[0].brand
          this.chartData4.series[1].name = chart4[1].brand

          this.chartData1.legend.push({name: chart1[0].brand, icon: 'rect'})
          this.chartData1.legend.push({name: chart1[1].brand, icon: 'rect'})
          this.chartData2.legend.push({name: chart2[0].brand, icon: 'rect'})
          this.chartData2.legend.push({name: chart2[1].brand, icon: 'rect'})
          this.chartData3.legend.push({name: chart3[0].brand, icon: 'rect'})
          this.chartData3.legend.push({name: chart3[1].brand, icon: 'rect'})
          this.chartData4.legend.push({name: chart4[0].brand, icon: 'rect'})
          this.chartData4.legend.push({name: chart4[1].brand, icon: 'rect'})

          for (var i = 0; i < chart1.length; i++) {
            if (i % 2 === 0) {
              this.chartData1.xAxisData.push(chart1[i].date)
              this.chartData1.series[0].data.push(chart1[i].sale)
            } else if (i % 2 === 1) {
              this.chartData1.series[1].data.push(chart1[i].sale)
            }
          }

          for (var j = 0; j < chart2.length; j++) {
            if (j % 2 === 0) {
              this.chartData2.xAxisData.push(chart2[j].date)
              this.chartData2.series[0].data.push(chart2[j].sales_amount)
            } else if (j % 2 === 1) {
              this.chartData2.series[1].data.push(chart2[j].sales_amount)
            }
          }

          for (var k = 0; k < chart3.length; k++) {
            if (k % 2 === 0) {
              this.chartData3.xAxisData.push(chart3[k].date)
              this.chartData3.series[0].data.push(chart3[k].sales_prop)
            } else if (k % 2 === 1) {
              this.chartData3.series[1].data.push(chart3[k].sales_prop)
            }
          }

          for (var l = 0; l < chart4.length; l++) {
            if (l % 2 === 0) {
              this.chartData4.xAxisData.push(chart4[l].date)
              this.chartData4.series[0].data.push(chart4[l].sales_amount_prop)
            } else if (l % 2 === 1) {
              this.chartData4.series[1].data.push(chart4[l].sales_amount_prop)
            }
          }
        }, (res) => {
          this.timeoutStatus = true
        })
      }
    },
    events: {
//      接收filter组件回传回来的值，然后再根据值重新请求页面数据
      'filterConditions': function (filterConditions) {
//        this.UrlParams.department = filterConditions.department || ''
        this.UrlParams.category = filterConditions.category || ''
        this.UrlParams.category_id = this.defaultCategoryId = filterConditions.category_id || ''
        this.UrlParams.platform = filterConditions.platform || ''
        this.UrlParams.platform_id = filterConditions.platform_id || ''
        this.UrlParams.startTime = filterConditions.starttime || ''
        this.UrlParams.endTime = filterConditions.endtime || ''
        this.UrlParams.dateSeg = filterConditions.dateSeg || ''
        this.UrlParams.org_code = filterConditions.org_code || ''
        this.UrlParams.brand = ''
        this.UrlParams.brand1 = ''
        this.UrlParams.brand2 = ''
        this.getData(this.UrlParams)
      },
//      选择品牌后重新获取数据
      'chooseBrand': function (brandA, brandB) {
        this.UrlParams.brand1 = brandA
        this.UrlParams.brand2 = brandB
        this.UrlParams.brand = brandA + ',' + brandB
        this.getData(this.UrlParams)
      }
    },
    ready: function () {
      this.routeKey = this.$route.query.key
      this.getData(this.UrlParams)
    },
    created: function () {
      this.routeKey = this.$route.query.key
    }
  }
</script>

<style lang="scss" scoped>
  .the-sales-content {
    position: relative;
    margin-top: 8vh;
    margin-bottom: 8vh;

    .content{
      height: 65vh;
      overflow-y: scroll;
    }
  }

  .section {
    border-top: 1.3vh solid #f2f2f2;
  }

  .sub-title {
    height: 7.8vh;
    line-height: 7.8vh;
    padding-left: .7rem;
    font-size: .32rem;
    background: url('../../../../static/img/time-title2.png') no-repeat .3rem center / .28rem .26rem;
    border-bottom: 1px solid #f2f2f2;
  }

  .btnPage {
    position: fixed;
    left: 0;
    top: 50vh;
    width: 100%;
    z-index: 200;
  }

  .btn-previous {
    width: 0.4rem;
    height: 0.8rem;
    float: left;
    background: rgba(0, 0, 0, 0.2);
  }

  .btn-next {
    width: 0.4rem;
    height: 0.8rem;
    float: right;
    background: rgba(0, 0, 0, 0.2);
  }

  .filter-message-box{
    padding: 0rem .2rem .2rem;
    font-size: 0.28rem;
  }
</style>
